using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace StarRating
{
    public enum ActionMode { Disable, Enable, Javascript }
    public class StarRating : WebControl, IPostBackDataHandler
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }
        #region Custom Styles
        [Bindable(true)]
        [
        Category("Appearance"),
        DefaultValue(null),
        PersistenceMode(PersistenceMode.InnerProperty),
        Description("StyleProgress Style"),
        Localizable(true),
        EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))
        ]
        public string OffImage
        {
            get
            {
                object o = ViewState["OffImage"];
                string img = o == null ? "" : (string)o;
                return img.Contains("~/") ? img.Substring(img.IndexOf("~/") + 2).Trim() : img.Trim();
            }

            set
            {
                ViewState["OffImage"] = value;
            }
        }
        [Bindable(true)]
        [
        Category("Appearance"),
        DefaultValue(null),
        PersistenceMode(PersistenceMode.InnerProperty),
        Description("StyleProgress Style"),
        Localizable(true),
        EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(System.Drawing.Design.UITypeEditor))
        ]
        public string OnImage
        {
            get
            {
                object o = ViewState["OnImage"];
                string img = o == null ? "" : (string)o;
                return img.Contains("~/") ? img.Substring(img.IndexOf("~/") + 2).Trim() : img.Trim();
            }

            set
            {
                ViewState["OnImage"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int StarCount
        {
            get
            {
                return ((ViewState["StarCount"] == null) ? 5 : (int)ViewState["StarCount"]);
            }

            set
            {
                ViewState["StarCount"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Value
        {
            get
            {
                return ((ViewState["Value"] == null) ? 0 : (int)ViewState["Value"]);
            }

            set
            {
                ViewState["Value"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(ActionMode.Disable)]
        [Localizable(true)]
        public ActionMode Mode
        {
            get
            {
                return ((ViewState["Enabled"] == null) ? ActionMode.Enable : (ActionMode)ViewState["Enabled"]);
            }

            set
            {
                ViewState["Enabled"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("function(){}")]
        [Localizable(true)]
        public string JavaScript
        {
            get
            {
                return ((ViewState["JavaScript"] == null) ? "" : (string)ViewState["JavaScript"]);
            }

            set
            {
                ViewState["JavaScript"] = value;
            }
        }
        #endregion
        protected override void Render(HtmlTextWriter output)
        {
            try
            {
                if (Mode == ActionMode.Enable)
                {
                    output.Write(GetJavascript());
                    output.Write("<input name=\"" + UniqueID + "\" id=\"" + UniqueID + "_val\" type=\"hidden\" value=\"" + Value.ToString() + "\" />");
                }
                for (int i = 0; i < StarCount; i++)
                {
                    output.Write(GetImageString(i + 1));
                }
            }
            catch (Exception exc)
            {
                output.Write("Error in control. " + exc.Message);
            }
        }
        private string GetJavascript()
        {
            string js = "<script type=\"text/javascript\">";
            js += "function DH" + UniqueID + "(p, offset){for(v=1;v<=offset;v++)";
            js += "{document.getElementById(p + v).src=\"" + OnImage + "\";}}";
            js += "function HH" + UniqueID + "(p){var val=document.getElementById(p + 'val').value;";
            js += "for(v=1;v<=" + StarCount.ToString() + ";v++) {if(v > val)document.getElementById(p + v).src=\"" + OffImage + "\";}}";
            js += "function SR" + UniqueID + "(p, v){document.getElementById(p + 'val').value=v;}</script>";
            return js;
        }
        private string GetImageString(int i)
        {
            string s = "";
            string img = OffImage;
            if (i <= Value)
                img = OnImage;
            string enAttr = "";
            if (Mode == ActionMode.Enable)
                enAttr = "style=\"cursor: pointer\" onclick=\"SR" + UniqueID + "('" + UniqueID + "_', " + i.ToString() + ");" + JavaScript + "\" onmouseover=\"DH" + UniqueID + "('" + UniqueID + "_','" + i.ToString() + "');\" onmouseout=\"HH" + UniqueID + "('" + UniqueID + "_')\"";
            else if (Mode == ActionMode.Javascript)
                enAttr = "style=\"cursor: pointer\" onclick=\"" + JavaScript + "\"";
            s += "<img name=\"" + UniqueID + "_" + i.ToString() + "\" id=\"" + UniqueID + "_" + i.ToString() + "\" src=\"" + img + "\" " + enAttr + "/>";
            return s;
        }
        public virtual bool LoadPostData(string postDataKey, 
            NameValueCollection postCollection)
        {
            try
            {
                int presentValue = Value;
                int postedValue = Convert.ToInt32(postCollection[postDataKey]);

                if (presentValue != postedValue)
                {
                    Value = postedValue;
                    return true;
                }
            }
            catch (Exception exc)
            {
                Value = 0;
            }
            return false;
        }
        public virtual void RaisePostDataChangedEvent()
        {
            //OnTextChanged(EventArgs.Empty);
        }
    }
}
